package cn.yuanqiao.archive.management.utils;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 静态方法类
 * @author ccw
 *
 */
public class StringUtils {
	/**
	 * 判断字符串为NULL或者为""
	 * @param str
	 * @return 字符串为NULL或者为""返回ture,否则返回false
	 */
   public static boolean isNULLString(String str) {
	   boolean result = false;
	   if(null == str || str.trim().length() <= 0){
		   result = true;
	   }
	   return result;
   }
   
	/**
	 * 判断参数为NULL或者为""
	 * @param str
	 * @return 字符串为NULL或者为""返回ture,否则返回false
	 */
  public static boolean isNULL(Object obj) {
	   boolean result = false;
	   if(null == obj || String.valueOf(obj).trim().length() <= 0){
		   result = true;
	   }
	   return result;
  }  
  
   /**
    * 计算百分比
    * @param number 计算数
    * @param count 总数
    * @return 该数占总数的比例，格式为xx%
    */
   public static String getPercentString(int number, int count) {
	   if(count==0) return "0%";
	   double result = number*1.0/count;
	   NumberFormat nf = NumberFormat.getPercentInstance();     
	   nf.setMinimumFractionDigits(0); 
	   return nf.format(result);
   }
   
   /**
    * 计算百分比
    * @param number 计算数
    * @param count 总数
    * @return 该数占总数的比例，格式为xx%
    */
   public static String getPercentString(double number, double count) {
	   if(count==0) return "0.00%";
	   double result = number*1.0/count*100;
	   DecimalFormat df = new DecimalFormat("0.00");
	   return df.format(result)+"%";
   }
   
   /**
    * 将double改成百分数
    * @param number 计算数
    * @return 格式为xx%
    */
   public static String getPercentString(double number) {
	   if(number==0) return "0.00%";
	   DecimalFormat df=new DecimalFormat("0.00%");
	   
	   return df.format(number);
   }
   
   /**
    * 将double改成百分数
    * @param number 计算数
    * @return 格式为xx%
    */
   public static String getPercentString(String number) {
	   if(StringUtils.isNULLString(number))
		   return "0.00%";
	   if(number.endsWith("%")) {
		   return number;
	   }
	   DecimalFormat df=new DecimalFormat("0.00%");
	   return df.format(Double.parseDouble(number));
   }
   
   /**
    * 计算百分比
    * @param number 计算数
    * @param count 总数
    * @return 该数占总数的比例，格式为0.00
    */
   public static double getPercent(int number, int count) {
	   if(count==0) return 0.0;
	   double result = number*1.0/count;
	   return Double.valueOf(String.format("%.2f", result));
   }
   
   /**
    * 获取Timestamp类型的当前时间
    * @return
    */
   public static Timestamp getTimestamp(){
	   return new Timestamp(System.currentTimeMillis());
   }
   
   /**
    * 获取数组的最大值
    * @param arr
    * @return
    */
   public static int getMaxVal(int[] arr){
	   int maxVal = 0;
	   if(arr != null && arr.length > 0){
		   maxVal = arr[0];
		   for(int i = 0; i < arr.length - 1; i++){
			   for(int j = i + 1; j < arr.length; j++){
				   if(arr[i] < arr[j]){
					   maxVal = arr[j];
					   i = j;
					   break;
				   }
			   }
		   }
		   
	   }
	   return maxVal;
   }
   
   public static String[] stringTrim(String[] str) {
	   List<String> listStr = Arrays.asList(str);
	   listStr = listStringTrim(listStr);
	   return listStr.toArray(new String[listStr.size()]);
   }
   /**
    * 去除List中的空字符串 JDK1.8以后可用Stream来替代此方法
    * @author Gaolh
    * @param list
    * @return
    */
   public static List<String> listStringTrim(List<String> list) {
	   List<String> newList = new ArrayList<String>();
	   for (String str : list) {
		   if(!StringUtils.isNULLString(str)) {
			   newList.add(str);
		   }
	   }
	   return newList;
   }
   
   
   public static String	getIntString(String str) {
	   char[] strChar = str.toCharArray();
	   StringBuffer result = new StringBuffer();
	   for (int i = 0; i < strChar.length; i++) {
		   char intChar = strChar[i];
		   if(Character.isDigit(intChar)) {
			   result.append(intChar);
		   }
	   }
	   return result.toString();
   }
   
   public static String removeStr(String str,char c) {
	   StringBuffer newStr = new StringBuffer();
	   char[] strs = str.toCharArray();
	   for (int i = 0; i < strs.length; i++) {
		   if(c!=strs[i]) {
			   newStr.append(strs[i]);
		   }
	   }
	   return newStr.toString();
   }
   /**
    * 获取当前时间往后推送一周的时间
    * @return
    */
   public static String getNowWorkDate() {
	   SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
	   Calendar c = Calendar.getInstance();
	   c.setTime(new Date());
	   c.add(Calendar.DATE, 7);
	   return format.format(c.getTime());
   }
   /**
    * 获取当前时间的字符串，精确到秒
    * @return
    */
   public static String getSecondString(){
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String year = cal.get(Calendar.YEAR)+"";
		String month = cal.get(Calendar.MONTH)<9?"0"+(cal.get(Calendar.MONTH)+1):(cal.get(Calendar.MONTH)+1)+"";
		String day = cal.get(Calendar.DAY_OF_MONTH)<10?"0"+cal.get(Calendar.DAY_OF_MONTH):cal.get(Calendar.DAY_OF_MONTH)+"";
		String hour = cal.get(Calendar.HOUR_OF_DAY)<10?"0"+cal.get(Calendar.HOUR_OF_DAY):cal.get(Calendar.HOUR_OF_DAY)+"";
		String minute = cal.get(Calendar.MINUTE)<10?"0"+cal.get(Calendar.MINUTE):cal.get(Calendar.MINUTE)+"";
		String second = cal.get(Calendar.SECOND)<10?"0"+cal.get(Calendar.SECOND):cal.get(Calendar.SECOND)+"";
		return year+month+day+hour+minute+second;
	}
   /**
    * 给定字符串的编码
    * GB2312
    * ISO-8859-1
    * UTF-8
    * GBK
    * @param str
    * @return
    */
   public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
				String s = encode;
				return s; //是的话，返回“GB2312“，以下代码同理
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}
   
   public static List<String> listKeyTrim(List<String> list) {
	   List<String> newList = new ArrayList<String>(); 
	   for (String string : list) {
		   newList.add(org.apache.commons.lang3.StringUtils.deleteWhitespace(string));
	   }
	   return newList;
   }
   /**
    * 字符串数组转为字符串
    * @param strArr
    * @return
    */
   public static String convertArrayToString(String[] strArr) {
       if (strArr == null || strArr.length == 0) {
           return "";
       }
       String res = "";
       for (int i = 0, len = strArr.length; i < len; i++) {
           res += strArr[i];
           if (i < len - 1) {
               res += ",";
           }
       }
       return res;
   }  
   
   
   public static String trim(String str) {
	   str=str.trim();
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		System.out.println("str="+str+"888");
		return dest;
	}
   
   
}